﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Mvc;
using MvcCarConsume.Domain;
using MvcCarConsume.Domain.Entites;
using System.ComponentModel;
using MvcContrib.Pagination;

namespace MvcCarConsume.WebUI
{
    public class AdminViewController : BaseController
    {
        private IAdminService adminService;
        public AdminViewController(IAdminService adminService)
        {
            this.adminService = adminService;
        }
        //
        // GET: /AdminView/
        [Authorize(Roles = "Admin")]
        public ActionResult Index()
        {
            return View();
        }
        //

        //
        //GET: /AdminView/CarsMenage/
        [Authorize(Roles = "Admin")]
        public ActionResult CarsManage([DefaultValue(1)]int page)
        {
            VMAdminViewCars MvcCarDBCars = new VMAdminViewCars();
            MvcCarDBCars.LoadCars(adminService.CarsManage(),page);
            return View(MvcCarDBCars);
        }

        //
        //GET: /AdminView/OrderMenage/
        [Authorize(Roles = "Admin")]
        public ActionResult OrderManage([DefaultValue(1)]int page)
        {
            VMAdminViewOrder MvVarDBOrders = new VMAdminViewOrder();
            MvVarDBOrders.LoadCars(adminService.OrdersManage(), page);
            return View(MvVarDBOrders);
        }

        //
        // GET: /AdminView/Add
        [Authorize(Roles="Admin")]
        public ActionResult CarAdd()
        {
            return View();
        }

        //
        // POST: /AdminView/Add
        [HttpPost]
        [Authorize(Roles = "Admin")]
        public ActionResult CarAdd(AdminViewAddCars car)
        {
            Car Addcar = new Car();
            try
            {
                Addcar.CarName = car.CarName;
                Addcar.BrandId = car.BrandId;
                Addcar.TypeId = car.TypeId;
                Addcar.Count = car.Count;
                Addcar.Price = car.Price;
                Addcar.Description = car.Discription;
            }
            catch (Exception e)
            {
                TempData["Message"] = "Error:" + e.Message;
                return Redirect("CarAdd");
            }
            bool stat = adminService.AddCar(Addcar);
            if (stat)
            {
                TempData["Message"] = "Add Success!";
            }
            else
            {
                TempData["Message"] = "Add Wrong!";
            }
            return View("");
        }

       

       
        //
        // GET: /AdminView/Update/5
        [Authorize(Roles = "Admin")]
        public ActionResult CarEdit(int id)
        {
            AdminViewInCars MvcCarDBCars = new AdminViewInCars();
            var car = adminService.GetCar(id);
            MvcCarDBCars.CarId = car.CarId;
            MvcCarDBCars.CarName = car.CarName;
            MvcCarDBCars.TypeId = car.TypeId;
            MvcCarDBCars.BrandId = car.BrandId;
            MvcCarDBCars.Count = car.Count;
            MvcCarDBCars.Price = car.Price;
            MvcCarDBCars.Description = car.Description;
            return View("CarEdit", MvcCarDBCars);
        }

        //
        // POST: /AdminView/Update/5

        [HttpPost]
        [Authorize(Roles = "Admin")]
        public ActionResult CarEdit(int id, FormCollection collection)
        {
            TempData["Message"] = "";
            Car car = new Car();
            UpdateModel(car, collection.ToValueProvider());
            bool stat = adminService.SaveCar(car);
            if (stat)
            {
                TempData["Message"] = "Update Success!";
            }
            else
            {
                TempData["Message"] = "wrong data!";
            }
            AdminViewInCars MvcCarDBCar = new AdminViewInCars();
            UpdateModel(MvcCarDBCar, collection.ToValueProvider());
            return View("CarEdit", MvcCarDBCar);
            
        }

              

        //
        // GET: /AdminView/Delete/5
        [Authorize(Roles = "Admin")]
        public ActionResult Delete(int id , [DefaultValue(1)]int page)
        {
            bool stat = adminService.DeleteCar(id);
            if(stat)
            {
            }
            else
            {
                Response.Write("error delete");
            }

         VMAdminViewCars MvcCarDBCars = new VMAdminViewCars();
            MvcCarDBCars.LoadCars(adminService.CarsManage(), page);
            return View("carssource", MvcCarDBCars);
        }

        //
        //GET: /AdminView/OrderAdd
        [Authorize(Roles = "Admin")]
        public ActionResult OrderAdd()
        {
            return View();
        }

        //
        //POST: /AdminView/OrderAdd
        [HttpPost]
        [Authorize(Roles = "Admin")]
        public ActionResult OrderAdd(AdminViewInOrder order)
        {
            Order _order = new Order();
            try
            {
                _order.CarId = order.CarId;
                _order.Count = order.Count;
                _order.BuildDate = order.BuildDate;
                _order.BorrowDate = order.BorrowDate;
                _order.SendDate = order.SendDate;
                _order.UserName = order.UserName;
                _order.Price = order.Price;
                _order.State = order.State;
            }
            catch(Exception e)
            {
                TempData["Message"] = "Error:" + e.Message;
                return Redirect("OrderAdd");
            }
            bool stat = adminService.AddOrder(_order);
            if (stat)
            {
                TempData["Message"] = "Add Success!";
            }
            else
            {
                TempData["Message"] = "Add Wrong!";
            }
            return View();
        }

        //
        //GET: /AdminView/UpdateOrder/5
        [Authorize(Roles = "Admin")]
        public ActionResult OrderEdit(int id)
        {
            var Orders = adminService.GetOrder(id);
            AdminViewInOrder order = new AdminViewInOrder();
            order.OrderId = Orders.OrderId;
            order.CarId = Orders.CarId;
            order.Count = Orders.Count;
            order.BuildDate = Orders.BuildDate;
            order.BorrowDate = Orders.BorrowDate;
            order.SendDate = Orders.SendDate;
            order.UserName = Orders.UserName;
            order.Price = Orders.Price;
            order.State = Orders.State;
            return View(order);
        }

        //
        //POST: /AdminView/UpdateOrder/5
        [HttpPost]
        [Authorize(Roles = "Admin")]
        public ActionResult OrderEdit(int id, FormCollection collection)
        {
            TempData["Message"] = "";
            Order order = new Order();
            UpdateModel(order, collection.ToValueProvider());
            bool stat = adminService.SaveOrder(order);
            if(stat)
            {
                TempData["Message"] = "Update Success!";
            }
            else
            {
                TempData["Message"] = "wrong data!";
            }
            AdminViewInOrder AdminVieworder = new AdminViewInOrder();
            UpdateModel(AdminVieworder, collection.ToValueProvider());
            return View("OrderEdit", AdminVieworder);
        }

        //
        //GET: /AdminView/DeleteOrder/5

        [Authorize(Roles = "Admin")]
        public ActionResult DeleteOrder(int id, [DefaultValue(1)]int page)
        {
            bool stat = adminService.DeleteOrder(id);
            if (stat)
            {
            }
            else
            {
                Response.Write("error delete");
            }
            VMAdminViewOrder MvVarDBOrders = new VMAdminViewOrder();
            MvVarDBOrders.LoadCars(adminService.OrdersManage(),page);
            return View("ordersource", MvVarDBOrders);
 
        }

    }
}
